package com.englisheveryday.api;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.cop.CopResponse;
import com.cop.annotation.ServiceMethod;
import com.englisheveryday.request.ArticleAddRequest;
import com.englisheveryday.request.ArticleSyncRequest;
import com.englisheveryday.request.ArticleUpdateRequest;
import com.englisheveryday.request.CategoryAddRequest;
import com.englisheveryday.request.CategorySyncRequest;
import com.englisheveryday.request.CategoryUpdateRequest;
import com.englisheveryday.request.CommonIDRequest;
import com.englisheveryday.request.FeedBackAddRequest;
import com.englisheveryday.request.QueryByPageRequest;
import com.englisheveryday.request.SystemupdateAddRequest;
import com.englisheveryday.response.ArticleListResponse;
import com.englisheveryday.response.ArticleResponse;
import com.englisheveryday.response.CategoryListResponse;
import com.englisheveryday.response.CategoryResponse;
import com.englisheveryday.response.CommonResultResponse;
import com.englisheveryday.response.FeedBackListResponse;
import com.englisheveryday.response.SystemUpdateListResponse;
import com.englisheveryday.response.SystemUpdateResponse;
import com.englisheveryday.service.ArticleService;
import com.englisheveryday.service.CategoryService;
import com.englisheveryday.service.SystemService;

/**
 * <pre>
 *    
 * </pre>
 * @author 马永华
 * @date 2012-9-16 上午10:59:23
 * @version 1.1
 */
@Service
public class API {
	
	@Autowired
	private ArticleService articleService;
	@Autowired
	private SystemService systemService;
	@Autowired
	private CategoryService categorysService;
	
	//口语
	@ServiceMethod(value="com.englisheveryday.article.sync",version="1.0")
	public CopResponse syncEveryday(ArticleSyncRequest request){
		ArticleListResponse response = new ArticleListResponse();
		try{
			response.setList(articleService.syncArticle(request.getLastAnchor(),request.getNextAnchor()));
		}catch(Exception e){
			e.printStackTrace();
		}
		
		return response;
	}
	
	@ServiceMethod(value="com.englisheveryday.article.query",version="1.0")
	public CopResponse query_article(QueryByPageRequest request){
		ArticleListResponse response = new ArticleListResponse();
		try{
			response.setList(articleService.query_article(request.getPage(), request.getCount()));
		}catch(Exception e){
			e.printStackTrace();
		}
		
		return response;
	}
	
	
	@ServiceMethod(value="com.englisheveryday.article.add",version="1.0")
	public CopResponse add_article(ArticleAddRequest request){
		CommonResultResponse response = new CommonResultResponse();
		try{
			response.setResult(articleService.add_article(request.getEnglishtext(), request.getTitle(), request.getExplain(), request.getCategory(), request.getAudio()));
		}catch(Exception e){
			e.printStackTrace();
		}
		
		return response;
	}
	
	@ServiceMethod(value="com.englisheveryday.article.get",version="1.0")
	public CopResponse get_article(CommonIDRequest request){
		ArticleResponse response = new ArticleResponse();
		try{
			response = articleService.get_article(request.getId());
		}catch(Exception e){
			e.printStackTrace();
		}
		
		return response;
	}
	
	
	@ServiceMethod(value="com.englisheveryday.article.delete",version="1.0")
	public CopResponse delete_article(CommonIDRequest request){
		CommonResultResponse response = new CommonResultResponse();
		try{
			response.setResult(articleService.delete_article(request.getId()));
		}catch(Exception e){
			e.printStackTrace();
		}
		
		return response;
	}
	
	@ServiceMethod(value="com.englisheveryday.article.update",version="1.0")
	public CopResponse update_article(ArticleUpdateRequest request){
		CommonResultResponse response = new CommonResultResponse();
		try{
			response.setResult(articleService.update_article(request.getId(),request.getEnglishtext(), request.getTitle(), request.getExplain(), request.getCategory(), request.getAudio()));
		}catch(Exception e){
			e.printStackTrace();
		}
		
		return response;
	}
	
	//category
	@ServiceMethod(value="com.englisheveryday.category.sync",version="1.0")
	public CopResponse syncCategory(CategorySyncRequest request){
		CategoryListResponse response = new CategoryListResponse();
		try{
			response.setList(categorysService.syncCategory(request.getLastAnchor(),request.getNextAnchor()));
		}catch(Exception e){
			e.printStackTrace();
		}
		return response;
	}
	
	@ServiceMethod(value="com.englisheveryday.category.query",version="1.0")
	public CopResponse query_category(QueryByPageRequest request){
		CategoryListResponse response = new CategoryListResponse();
		try{
			response.setList(categorysService.query_category(request.getPage(), request.getCount()));
		}catch(Exception e){
			e.printStackTrace();
		}
		return response;
	}
	
	
	@ServiceMethod(value="com.englisheveryday.category.add",version="1.0")
	public CopResponse add_category(CategoryAddRequest request){
		CommonResultResponse response = new CommonResultResponse();
		try{
			response.setResult(categorysService.add_category(request.getName()));
		}catch(Exception e){
			e.printStackTrace();
		}
		return response;
	}
	
	@ServiceMethod(value="com.englisheveryday.category.get",version="1.0")
	public CopResponse get_category(CommonIDRequest request){
		CategoryResponse response = new CategoryResponse();
		try{
			response = categorysService.get_category(request.getId());
		}catch(Exception e){
			e.printStackTrace();
		}
		
		return response;
	}
	
	
	@ServiceMethod(value="com.englisheveryday.category.delete",version="1.0")
	public CopResponse delete_category(CommonIDRequest request){
		CommonResultResponse response = new CommonResultResponse();
		try{
			response.setResult(categorysService.delete_category(request.getId()));
		}catch(Exception e){
			e.printStackTrace();
		}
		return response;
	}
	
	@ServiceMethod(value="com.englisheveryday.category.update",version="1.0")
	public CopResponse update_category(CategoryUpdateRequest request){
		CommonResultResponse response = new CommonResultResponse();
		try{
			response.setResult(categorysService.update_category(request.getId(),request.getName()));
		}catch(Exception e){
			e.printStackTrace();
		}
		return response;
	}
	
	//用户反馈
	@ServiceMethod(value="com.englisheveryday.feedback.add",version="1.0")
	public CopResponse save_feedback(FeedBackAddRequest request){
		CommonResultResponse response = new CommonResultResponse();
		try{
			response.setResult(systemService.save_feedBack(request.getContent()));
		}catch(Exception e){
			e.printStackTrace();
		}

		return response;
	}
	
	//获得新版本
	@ServiceMethod(value="com.englisheveryday.systemupdate.get",version="1.0")
	public CopResponse get_systemupdate(){
		SystemUpdateResponse response = null;
		try{
			response =  systemService.get_systemUpdate();
		}catch(Exception e){
			e.printStackTrace();
		}
		return response;
	}
	
	//查询新版本
	@ServiceMethod(value="com.englisheveryday.systemupdate.query",version="1.0")
	public CopResponse query_systemupdate(QueryByPageRequest request){
		SystemUpdateListResponse response = new SystemUpdateListResponse();
		try{
			response.setList(systemService.query_systemUpdate(request.getPage(), request.getCount()));
		}catch(Exception e){
			e.printStackTrace();
		}
		
		return response;
	}
	
	//查询用户反馈
	@ServiceMethod(value="com.englisheveryday.feedback.query",version="1.0")
	public CopResponse query_feedback(QueryByPageRequest request){
		FeedBackListResponse response = new FeedBackListResponse();
		try{
			response.setList(systemService.query_feedback(request.getPage(), request.getCount()));
		}catch(Exception e){
			e.printStackTrace();
		}
		return response;
	}
	
	@ServiceMethod(value="com.englisheveryday.feedback.delete",version="1.0")
	public CopResponse delete_feedback(CommonIDRequest request){
		CommonResultResponse response = new CommonResultResponse();
		try{
			response.setResult(systemService.delete_feedback(request.getId()));
		}catch(Exception e){
			e.printStackTrace();
		}
		return response;
	}
	
	@ServiceMethod(value="com.englisheveryday.systemupdate.add",version="1.0")
	public CopResponse add_systemupdate(SystemupdateAddRequest request){
		CommonResultResponse response = new CommonResultResponse();
	try{
		response.setResult(systemService.save_systemupdate(request.getNowversion(), request.getUrl()));
		}catch(Exception e){
			e.printStackTrace();
		}
		return response;
	}
	
	@ServiceMethod(value="com.englisheveryday.systemupdate.delete",version="1.0")
	public CopResponse delete_systemupdate(CommonIDRequest request){
		CommonResultResponse response = new CommonResultResponse();
	   try{
		   response.setResult(systemService.delete_systemupdate(request.getId()));
		}catch(Exception e){
			e.printStackTrace();
		}
		return response;
	}
	
	
}

